package com.kehutong.charge.admin;

import com.kehutong.charge.entity.ChargeTypeConfig;
import com.kehutong.charge.entity.ChargeTypeTemp;
import com.kehutong.charge.service.ChargeTypeService;
import com.kehutong.charge.service.ChargeTypeTempService;
import com.kehutong.common.DeptService;
import com.kehutong.common.constant.CommonConstant;
import com.kehutong.common.dto.DeptDTO;
import com.kehutong.common.util.Token;
import org.coraframework.authz.HasPermission;
import org.coraframework.bean.Beans;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.orm.Pool;

import java.util.List;

/**
 * @program: kht
 * @description: 正常/临时收费类型配置
 * @author: joneron
 * @create: 2021/6/21
 **/
@MvcAction
@ReqMapping("/admin/pay/charge/type")
public class ChargeTypeController {

    private static final Logger logger = LoggerFactory.getLogger(ChargeTypeController.class);

    @Inject
    DeptService deptService;
    @Inject
    ChargeTypeService chargeTypeService;
    @Inject
    ChargeTypeTempService chargeTypeTempService;





    @ReqMapping("/page")
    @HasPermission("pay:charge:type:view")
    public Object page(JSONObject jsonObject) {
        return chargeTypeService.findPage(jsonObject);
    }


    @ReqMapping("/list")
    @HasPermission("pay:charge:type:view")
    public Object list(JSONObject jsonObject) {
        return chargeTypeService.findList(jsonObject);
    }


    @Bean(newInstance=true, copy=true)
    @HasPermission("pay:charge:type:edit")
    @ReqMapping("/save")
    public Object save(ChargeTypeConfig entity, Token token) throws Exception {
        entity.setCreateName(token.getUsername());
        List<String> departList = token.getDepartments();
        if(null != departList && departList.size() > 0){
            logger.error("用户{}数据异常，部门为空！",token.getUsername());

            DeptDTO deptDTO = deptService.getDept(departList.get(0));
            entity.setDepartmentNo(deptDTO.getDepartmentNo());
            entity.setDepartmentNos(deptDTO.getDepartmentNos());
            entity.setDepartmentName(deptDTO.getDepartmentName());
        }
        return chargeTypeService.save(entity);
    }


    @Bean(copy=true)
    @HasPermission("pay:charge:type:edit")
    @ReqMapping("/update")
    public Object update(ChargeTypeConfig entity) throws Exception {

        return chargeTypeService.update(entity);
    }


    @HasPermission("pay:charge:type:edit")
    @ReqMapping("/delete")
    public Object delete(ChargeTypeConfig entity) {

        return chargeTypeService.deleteById(entity.getId());
    }


    @ReqMapping("/get")
    public Object get(ChargeTypeConfig entity) {

        return Result.success(entity);
    }


    @ReqMapping("/setStatus")
    @HasPermission("pay:charge:type:edit")
    public Object setStatus(ChargeTypeConfig entity) throws Exception {
        entity.setStatus(!entity.isStatus());
        chargeTypeService.update(entity);
        return Result.success();
    }


    /**临时收费类型模块**/
    @ReqMapping("/pageTemp")
    @HasPermission("pay:charge:type:temp:view")
    public Object pageTemp(JSONObject jsonObject) {
        return chargeTypeTempService.findPage(jsonObject);
    }


    @ReqMapping("/listTemp")
    @HasPermission("pay:charge:type:temp:view")
    public Object listTemp(JSONObject jsonObject) {
        return chargeTypeTempService.findList(jsonObject);
    }


    @ReqMapping("/saveTemp")
    public Object saveTemp(JSONObject entity, Token token) throws Exception {

        ChargeTypeTemp chargeTypeTemp = Pool.newInstance(ChargeTypeTemp.class);
        chargeTypeTemp.setFeeTypeName(entity.getString("feeTypeName"));
        chargeTypeTemp.setInvoice(entity.getBoolean("invoice"));
        chargeTypeTemp.setInvoiceTax(entity.getBigDecimal("invoiceTax"));

        chargeTypeTemp.setCreateName(token.getUsername());
        List<String> departList = token.getDepartments();
        if(null != departList && departList.size() > 0){
            logger.error("用户{}数据异常，部门为空！",token.getUsername());

            DeptDTO deptDTO = deptService.getDept(departList.get(0));
            chargeTypeTemp.setDepartmentNo(deptDTO.getDepartmentNo());
            chargeTypeTemp.setDepartmentNos(deptDTO.getDepartmentNos());
            chargeTypeTemp.setDepartmentName(deptDTO.getDepartmentName());
        }
        return chargeTypeTempService.save(chargeTypeTemp);
    }


    @Bean(copy=true)
    @HasPermission("pay:charge:type:temp:edit")
    @ReqMapping("/updateTemp")
    public Object updateTemp(ChargeTypeTemp entity) throws Exception {

        return chargeTypeTempService.update(entity);
    }


    @HasPermission("pay:charge:type:temp:edit")
    @ReqMapping("/deleteTemp")
    public Object deleteTemp(ChargeTypeTemp entity) {

        return chargeTypeTempService.deleteById(entity.getId());
    }


    @ReqMapping("/getTemp")
    public Object getTemp(ChargeTypeTemp entity) {

        return Result.success(entity);
    }


    @ReqMapping("/setTempStatus")
    @HasPermission("pay:charge:type:temp:edit")
    public Object setTempStatus(ChargeTypeTemp entity) throws Exception {
        entity.setStatus(!entity.isStatus());
        chargeTypeTempService.update(entity);
        return Result.success();
    }
}
